package saucaca.Adapter;

import java.util.List;

import saucaca.Object.AlbumObject;
import saucaca.Util.AppController;
import saucaca.fragment.AlbumDetailFargment;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

import com.android.volley.toolbox.ImageLoader;
import com.lemon.kenhnhacvn.BaseActivity;
import com.lemon.kenhnhacvn.MainActivity;
import com.lemon.kenhnhacvn.R;

public class HorizontalAdapter {
	private Context context;
	private List<AlbumObject> albums = null;
	LinearLayout linearGallery;

	ImageLoader imageLoader;

	public HorizontalAdapter(Context c, LinearLayout linearGallery, List<AlbumObject> albums) {
		context = c;
		this.linearGallery = linearGallery;
		try {
			this.linearGallery.removeAllViews();
		} catch (Exception e) {
		}
		this.albums = albums;
		Show();
	}

	public void Show() {
		for (int i = 0; i < albums.size(); i++) {
			getView(albums.get(i), linearGallery);
		}
	}

	public void getView(AlbumObject item, LinearLayout layoutgallery) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		ViewHolder holder;
		View convertView = inflater.inflate(R.layout.item_album_gridview, null);
		holder = new ViewHolder();
		holder.layout = (LinearLayout) convertView.findViewById(R.id.item_album_layout);
		holder.title = (TextView) convertView.findViewById(R.id.custom_view_album_Title);
		holder.artist = (TextView) convertView.findViewById(R.id.custom_view_album_artist);
		holder.image = (ImageView) convertView.findViewById(R.id.custom_view_album_image);

		holder.title.setText(item.getTXNAME());
		holder.artist.setText(item.getNAME_SINGERS());
		final AlbumObject albumitem = item;
		holder.layout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				((MainActivity)context).switchContent(AlbumDetailFargment.class.getName(), albumitem);
				
			}
		});
		// Picasso.with(context).load(item.getAVATAR()).placeholder(R.drawable.ic_empty).error(R.drawable.ic_error).transform(new
		// RoundTransform()).into(holder.image);
		holder.image.setLayoutParams(new LayoutParams(BaseActivity.linear_params_3.width, BaseActivity.linear_params_3.height));
//		imageLoader.displayImage(item.getAVATAR(), holder.image, options);
		try {
			imageLoader=((BaseActivity)context).imageLoader;
		} catch (Exception e) {
			imageLoader=AppController.getInstance().getImageLoader();
		}
		
		imageLoader.get(item.getAVATAR(), ImageLoader.getImageListener(holder.image,
	         R.drawable.ic_default, R.drawable.ic_default));
		android.widget.LinearLayout.LayoutParams Params = new android.widget.LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		Params.setMargins(5, 5, 5, 5);
		convertView.setLayoutParams(Params);
		layoutgallery.addView(convertView);
	}

	public class ViewHolder {
		LinearLayout layout;
		public TextView title;
		public TextView artist;
		public ImageView image;
	}

}
